<script>
import Vue from 'vue'
import { Icon } from 'vant'
import store from 'store2'
Vue.use(Icon)

export default {
  props: {
    keywords: String
  },

  data() {
    return {
      inputValue: this.keywords
    }
  },
  
  methods: {
    back() {
      this.$router.back()
    },

    search() {
      // 为了 Debug 重复路由提交
      if (this.$route.query.keywords !== this.inputValue) {
        this.$router.replace({
          name: 'list',
          query: {
            keywords: this.inputValue
          }
        })

        // 搜索关键字的存储
        if (this.inputValue !== '') {
          const data = store.get('cookbook-store') || []
          data.push(this.inputValue)
          store.set('cookbook-store', data)
        }
      }
    }
  }
}
</script>

<template>
  <div class="searchbar-container">
    <div>
      <van-icon 
        name="arrow-left"
        size=".2rem"
        @click="back"
      />
    </div>
    <div>
      <van-icon name="search" size=".2rem" />
      <input v-model="inputValue" type="text">
    </div>
    <div @click="search">搜索</div>
  </div>
</template>

<style lang="scss" scoped>
.searchbar-container {
  height: .44rem;
  background: #ee742f;
  display: flex;
  font-size: .16rem;
  div {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    &:first-child {
      width: .4rem;
    }
    &:nth-child(2) {
      flex: 1;
      position: relative;
      input {
        background: #d55f27;
        border: 0;
        border-radius: .06rem;
        width: 100%;
        height: .3rem;
        color: #fff;
        padding: 0 .1rem 0 .35rem;
      }
      .van-icon-search {
        position: absolute;
        left: .1rem;
        top: .12rem;
      }
    }
    &:last-child {
      width: .6rem;
    }
  }
} 
</style>
